#include"head.h"
LtNode* ButNode(LtDataType x)
{
	LtNode* node = (LtNode*)malloc(sizeof(LtNode));
	if (node == NULL)
	{
		perror("malloc(LtNode)");
		exit(1);
	}
	node->next =node->prev= node;
	node->val = x;
	return node;
}


LtNode* LtInit()

{
	LtNode* phead = ButNode(-1);
	return phead;
}

void LtPushBack(LtNode* phead, LtDataType x)
{
	assert(phead);
    LtNode* newnode=ButNode(x);
	newnode->prev = phead->prev;
	newnode->next = phead;
	phead->prev->next = newnode;
	phead->prev =newnode;
}

void LtPrint(LtNode* phead)
{
	assert(phead);
	LtNode* pcur = phead->next;
	while (pcur != phead)
	{
		printf("%d", pcur->val);
		printf("->");
		pcur = pcur->next;
	}
	printf("\n");
}

void LtPushFront(LtNode* phead, LtDataType x)
{
	assert(phead);
	LtNode* newnode = ButNode(x);
	newnode->next = phead->next;
	newnode->prev = phead;
	phead->next->prev = newnode;
	phead->next = newnode;
}

bool LtEmpty(LtNode* phead)
{
	assert(phead);
	return phead == phead->next;
}

void LtPopBack(LtNode* phead)
{
	assert(!LtEmpty(phead));
	LtNode* del = phead->prev;
	phead->prev = del->prev;
	del->prev->next = phead;
	free(del);
	del = NULL;
}

void LtPopFront(LtNode* phead)
{
	assert(!LtEmpty(phead));
	LtNode* del = phead->next;
	phead->next = del->next;
	del->next->prev = phead;
	free(del);
	del = NULL;
}

LtNode* LtPopFind(LtNode* phead, LtDataType x)
{
	assert(phead);
	LtNode* pcur = phead->next;
	while (pcur != phead)
	{
		if (pcur->val == x)
			return pcur;
		pcur = pcur->next;
	}
	return NULL;
}

void LtInsert(LtNode* pos, LtDataType x)
{
	assert(pos);
	LtNode* del = ButNode(x);
	del->prev = pos;
	del->next = pos->next;
	pos->next->prev = del;
	pos->next = del;
}

void  LTErase(LtNode* pos)
{
	assert(pos);
	pos->next->prev = pos->prev;
	pos->prev->next = pos->next;
	free(pos);
	pos = NULL;
}

void LtDesTroy(LtNode* phead)
{
	LtNode* pcur = phead->next;
	while (pcur != phead)
	{
		LtNode* next = pcur->next;
		free(pcur);
		pcur = next;
	}
	free(phead);
	phead = NULL;
}
int main()
{
	LtNode* phead = LtInit();
	LtPushBack(phead,1);
	LtPushBack(phead,2);
	LtPushBack(phead,3);
	LtPushBack(phead,4);
	LtPushBack(phead,5);
	LtPushFront(phead, 100);
	LtPushFront(phead, 102);
	//LtPopBack(phead);
	LtPopFront(phead);
	LtPopFront(phead);
	LtPopFront(phead);
	LtPopFront(phead);
	LtPopFind(phead, 100);
	LtPrint(phead);
	LtDesTroy(phead);
	phead = NULL;
	return 0;
}